package bean;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class MemberServlet extends HttpServlet {
	private static final long serialVersionUID=1L;
	Connection conn;
	ArrayList<MemberVo> al;//jstl type로 뿌리기위해
	StringBuilder sb;//request
	
	public MemberServlet() {
		super();
		try
		{
			conn=new MyOracle().getConn();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		
		PrintWriter out = resp.getWriter();
		
		String findstr = request.getParameter("findstr");
		String sql = "select * from member where mid like ? or irum like ?";
		PreparedStatement ps=null;
		ResultSet rs=null;
		try
		{
			ps=conn.prepareStatement(sql);
			ps.setString(1, "%"+findstr+"%");
			ps.setString(2, "%"+findstr+"%");
			rs=ps.executeQuery();
//			while(rs.next())
//			{
//				out.print("<li>"+rs.getString("mid")+"["+rs.getString("irum")+"]");
//			}
			sb = new StringBuilder();
			al = new ArrayList<MemberVo>();
			while(rs.next())
			{
				String temp = new String().format("<li>%s[%s]", rs.getString("mid"), rs.getString("irum"));
				sb.append(temp);
				
				MemberVo vo = new MemberVo();
				vo.setMid(rs.getString("mid"));
				vo.setIrum(rs.getString("irum"));
				al.add(vo);
			}
			//현재 페이지에 검색값을 나타내기위함..
			request.setAttribute("sb", sb);
			
			request.setAttribute("al", al);
			
			ServletContext context = getServletContext();
			RequestDispatcher dispatcher = context.getRequestDispatcher("/servlet/member_list.jsp");
			dispatcher.forward(request, resp);
			
			rs.close();
			ps.close();
			out.close();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
}


























